<?php

namespace app\admin\dao;

use app\common\model\PromotionCallback;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * PromotionCallbackDao 数据访问对象
 * Class PromotionCallbackDao
 * @package app\admin\dao
 * @method PromotionCallback findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method PromotionCallback insertActive(array $params, array $allow_field = [])
 * @method PromotionCallback updateActive(PromotionCallback $model, array $params, array $allow_field = [])
 * @method PromotionCallback findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method PromotionCallback updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(PromotionCallback $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class PromotionCallbackDao
{
    use DaoTrait;

    /**
     * @var PromotionCallback
     */
    private $baseModel;

    /**
     * PromotionCallbackDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = PromotionCallback::class;
    }

    public function list($promotionGroupId, $promotionPlatform, $startTime, $endTime, ?int $page, ?int $page_size): array
    {
        $query = PromotionCallback::with([
            'promotionChain',
            'promotionGroup',
            'promotionAction',
            'promotionEvent',
        ])
            ->when(is_id($promotionGroupId), fn($query) => $query->where('promotionGroupId', $promotionGroupId))
            ->when(not_blank($promotionPlatform), fn($query) => $query->where('promotionPlatform', $promotionPlatform))
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->order('id', 'DESC')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }
}
